//
//  AXHomeNzscVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/22.
//  Copyright © 2018年 tzhg. All rights reserved.
//

#import "AXHomeNzscVC.h"
#import "LKCircleScrollView.h"
#import "Header.h"
#import "ZXCategories.h"
#import "UIView+SDAutoLayout.h"
#import "AXHomeNzscFirstCell.h"
#import "AXHomeNzscSecondCell.h"
#import "AXHomeNzscThirdCell.h"
#import "WHRAgriculturalCapitalClassifyVC.h"
#import "BaseShowViewController.h"
//#import "ServiceViewController.h"
#import "AXNzscPgbVC.h"
#import "AXNzscNztjVC.h"


@interface AXHomeNzscVC ()<UITableViewDelegate,UITableViewDataSource,LKCircleScrollViewDelegate, LKCircleScrollViewDataSource,UISearchBarDelegate>
/** headView */
@property (nonatomic ,strong) UIView *headView;
@property (nonatomic, strong) UITableView *tableView;
/** 广告图片 */
@property (nonatomic ,strong) UIImageView *advImageView;
/** 循环滚动View */
@property (nonatomic, strong) LKCircleScrollView *circleScrollView;
@property (nonatomic, strong) NSArray<NSString *> *scrollViewImageNames;

@end

@implementation AXHomeNzscVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatView];
    [self setlayout];
    [self setupNavigationItems];
    self.tableView.tableHeaderView = _headView;
    [_headView setNeedsLayout];
    [_headView layoutIfNeeded];//刷新布局
    CGFloat height = [_headView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    _headView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, height);//headView 的大小
    _headView.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = _headView;
    
}

- (void)creatView {
    
    _headView = [[UIView alloc]init];
    
    //添加scrollview
    self.scrollViewImageNames = @[@"shcm", @"sg2.jpeg.jpeg", @"sg3.jpeg.jpeg"];
    //self.circleScrollView = [[LKCircleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    self.circleScrollView.isAutoScroll = NO;
    self.circleScrollView = [[LKCircleScrollView alloc]init];
    self.circleScrollView.isAutoScroll = NO;
    self.circleScrollView.delegate = self;
    self.circleScrollView.dataSource = self;
    [self.headView addSubview:self.circleScrollView];
    
    
    self.tableView = [[UITableView alloc]init];
    /** 去掉cell线 */
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    //注册cell
    [self.tableView registerClass:[AXHomeNzscFirstCell class] forCellReuseIdentifier:[AXHomeNzscFirstCell reuseID]];
    [self.tableView registerClass:[AXHomeNzscSecondCell class] forCellReuseIdentifier:[AXHomeNzscSecondCell reuseID]];
    [self.tableView registerClass:[AXHomeNzscThirdCell class] forCellReuseIdentifier:[AXHomeNzscThirdCell reuseID]];

    _headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 129);
    self.tableView.tableHeaderView = self.headView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

- (void)setlayout {
    
    if (_isHeight) {
        self.tableView.sd_layout
        .leftSpaceToView(self.view, 0)
        .topSpaceToView(self.view, 0)
        .widthIs(Wi)
        .heightIs(SCREEN_HEIGHT);
    } else {
        self.tableView.sd_layout
        .leftSpaceToView(self.view, 0)
        .topSpaceToView(self.view, 0)
        .widthIs(Wi)
        .heightIs(Hi-Hs-Htb-44);
    }

    
    self.circleScrollView.sd_layout
    .leftSpaceToView(self.headView,0)
    .rightSpaceToView(self.headView,0)
    .topSpaceToView(self.headView,0)
    .heightIs(129);
    
    [_headView setupAutoHeightWithBottomView:self.circleScrollView bottomMargin:0];
    
}



- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 30, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    /** 搜索 */
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"农资商城";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT_EIGHTEEN;
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    self.navigationItem.titleView = titleLabel;
    
    
}

#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - tableViewDelegate & tableViewDatasorce
/** cell 的点击方法 */


//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

/** cell的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return 80;
        
    }else if (indexPath.section == 1) {
        
        return 400;
        
    }
    
    return 120;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //农资商城、农资推荐、服务网点、药效实验、排行榜
    if (indexPath.section == 0) {
        
        AXHomeNzscFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeNzscFirstCell reuseID] forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
        cell.tapButtonClickedBlock = ^(NSInteger index) {
           
            if (index == 0) {
                
                WHRAgriculturalCapitalClassifyVC *vc = [[ WHRAgriculturalCapitalClassifyVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }else if (index ==1) {
                
             
                AXNzscNztjVC *vc = [[AXNzscNztjVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
           
                
                
            }else if (index == 2){
                
                
//                ServiceViewController *vc = [[ServiceViewController alloc]init];
//                [self.navigationController pushViewController:vc animated:YES];
            
            }
            else if (index == 3){
                
                BaseShowViewController *vc = [[BaseShowViewController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
              
                
            }
            //排行榜
            else if (index == 4) {
               
                 AXNzscPgbVC *vc = [[AXNzscPgbVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }
        };
        
        return cell;
        
        return cell;
    }
    /** 菜单cell */
    if (indexPath.section == 1) {
        AXHomeNzscSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeNzscSecondCell reuseID] forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        
        return cell;
    }
    
   
    AXHomeNzscThirdCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeNzscThirdCell reuseID] forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
    
}



#pragma mark - Circle Scroll View Data Source & Delegate

- (NSInteger)numberOfViewsInCircleScrollView:(LKCircleScrollView *)circleScrollView {
    
    return self.scrollViewImageNames.count;
}

/** 视图来源 */
- (UIView *)circleScrollView:(LKCircleScrollView *)circleScrollView viewAtIndex:(NSInteger)index {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.scrollViewImageNames[index]]];
    imageView.contentMode =UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds=YES;//
    
    if (index == 0) {
        
        imageView.userInteractionEnabled = YES;
        
        
        
    }
    
    
    return imageView;
}

/** 点击View时调用  */
- (void)circleScrollView:(LKCircleScrollView *)circleScrollView didTapViewAtIndex:(NSInteger)index {
    if (index == 0) {
        //事件不会触发，点击事件可能被ScrollView截获
        //        ZXVoteProductionListViewController *vc = [[ZXVoteProductionListViewController alloc] init];
        //        [self.navigationController pushViewController:vc animated:YES];
    }
}





- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
}


#pragma mark - UISearchBar delegate
//将要开始编辑时的回调，返回为NO，则不能编辑
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    
    return NO;
}


@end



